package com.winning.pmph.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winning.pmph.entity.Authority;
import com.winning.pmph.entity.Resource;
import com.winning.pmph.entity.Role;
import com.winning.pmph.mapper.RoleMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Transactional
public class RoleService extends ServiceImpl<RoleMapper, Role> {
    @javax.annotation.Resource
    private RoleMapper roleMapper;
    @javax.annotation.Resource
    private ResourceService resourceService;
    @javax.annotation.Resource
    private AuthorityService authorityService;

    public List<Role> getRoleList(String roleName) {
        List<Role> roleList = lambdaQuery()
                .like(StringUtils.isNotBlank(roleName), Role::getRoleName, roleName)
                .orderByDesc(Role::getUpdatedTime).list();
        for (Role role : roleList) {
            List<Authority> authorityList = authorityService.lambdaQuery().eq(StringUtils.isNotBlank(role.getId()), Authority::getRoleId, role.getId()).list();
            if (CollectionUtils.isNotEmpty(authorityList)){
                List<Resource> resourceList = resourceService.lambdaQuery()
                        .in(Resource::getId, authorityList.stream().map(Authority::getResourceId)
                                .collect(Collectors.toList())).list();
                String resourceName = resourceList.stream().map(Resource::getName).collect(Collectors.joining(","));
                role.setResourceName(resourceName);
            }
        }
        return roleList;
    }

    public List<Resource> selectResourceListByIdList(List<String> roleIds) {
        // 权限ID
        List<Authority> authorityList = authorityService.lambdaQuery().in(Authority::getRoleId, roleIds).list();
        List<String> resourceIds = authorityList.stream().map(Authority::getResourceId).collect(Collectors.toList());
        List<Resource> resourceList = resourceService.lambdaQuery().in(Resource::getId, resourceIds).orderByAsc(Resource::getSortNum).list();
        String administratorId = roleIds.stream().filter(t -> StringUtils.equals(t, Role.ROLE_ADMINISTRATOR_ID)).collect(Collectors.joining());
        Map<String, Authority> map = new HashMap<>();
        authorityList.forEach(t -> {
            map.put(t.getResourceId(), t);
        });
        resourceList.forEach(resource -> {
            resource.setPushButton(authorityList.stream().filter(authority -> resource.getId().equals(authority.getResourceId()))
                    .max(Comparator.comparingInt(Authority::getPushButton)).get().getPushButton() == 1);
            resource.setEditButton(authorityList.stream().filter(authority -> resource.getId().equals(authority.getResourceId()))
                    .max(Comparator.comparingInt(Authority::getEditButton)).get().getEditButton() == 1);
            resource.setDetailButton(authorityList.stream().filter(authority -> resource.getId().equals(authority.getResourceId()))
                    .max(Comparator.comparingInt(Authority::getDetailButton)).get().getDetailButton() == 1);
            resource.setRoleIds(administratorId);
        });
        return resourceList;
    }
}
